<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\System;
use App\Models\ApplyFile;
use Illuminate\Http\Request;
use OSS\OssClient;

class SystemController extends Controller
{
    public function __construct()
    {
        view()->share('menu', 'sys');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = [];
        $sys  = System::find(1);
        if ($sys) {
            $data['logo']        = config('alioss.host') .'/' . $sys->logo;
            $data['app_logo']    = config('alioss.host') . '/' .$sys->app_logo;
            $data['url']         = $sys->url;
            $data['title']       = $sys->title;
            $data['keyword']     = $sys->keyword;
            $data['mark_en']     = $sys->mark_en;
            $data['name']        = $sys->name;
            $data['exchange']    = $sys->exchange;
            $data['desc']        = $sys->desc;
            $data['copyright']   = $sys->copyright;
            $data['keep_record'] = $sys->keep_record;
            $data['address']     = $sys->address;
            $data['qq']          = $sys->qq;
            $data['tel']         = $sys->tel;
            $data['app_name']    = $sys->app_name;
        } else {
            $data['logo']        = '';
            $data['app_logo']    = '';
            $data['url']         = '';
            $data['title']       = '';
            $data['keyword']     = '';
            $data['mark_en']     = '';
            $data['name']        = '';
            $data['exchange']    = '';
            $data['desc']        = '';
            $data['copyright']   = '';
            $data['keep_record'] = '';
            $data['address']     = '';
            $data['qq']          = '';
            $data['tel']         = '';
            $data['app_name']    = '';
        }
        return view('admin/system/create', compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data                = [];
        $data['url']         = $request->get('url');
        $data['title']       = $request->get('title');
        $data['keyword']     = $request->get('keyword');
        $data['mark_en']     = $request->get('mark_en');
        $data['name']        = $request->get('name');
        $data['exchange']    = $request->get('exchange');
        $data['desc']        = $request->get('desc');
        $data['copyright']   = $request->get('copyright');
        $data['keep_record'] = $request->get('keep_record');
        $data['address']     = $request->get('address');
        $data['qq']     = $request->get('qq');
        $data['tel']     = $request->get('tel');
        $data['app_name']     = $request->get('app_name');
        //dd($request->all());
        $oss = new OssClient(config('alioss.accessKeyId'), config('alioss.accessKeySecret'), config('alioss.endpoint'));

        if ($request->has('logo')) {
            $logo = $request->file('logo');
            if ($logo) {
                if ($logo->isValid()) {
                    $realPath = $logo->getRealPath(); //临时文件的绝对路径
                    $ext      = $logo->getClientOriginalExtension(); // 扩展名
                    $filename = date('YmdHis') . '_' . uniqid() . '.' . $ext; //文件名
                    $osspath  = 'block_chain/' . $filename; //oss路径
                    $oss->uploadFile(config('alioss.bucket'), $osspath, $realPath); //上传文件到阿里云

                    $data['logo'] = $osspath; //数据库保存非全路径文件名
                }
            }
        }
        if ($request->has('app_logo')) {
            $logo = $request->file('app_logo');
            if ($logo) {
                if ($logo->isValid()) {
                    $realPath = $logo->getRealPath(); //临时文件的绝对路径
                    $ext      = $logo->getClientOriginalExtension(); // 扩展名
                    $filename = date('YmdHis') . '_' . uniqid() . '.' . $ext; //文件名
                    $osspath  = 'block_chain/' . $filename; //oss路径
                    $oss->uploadFile(config('alioss.bucket'), $osspath, $realPath); //上传文件到阿里云

                    $data['app_logo'] = $osspath; //数据库保存非全路径文件名
                }
            }
        }
        if ($request->has('path')) {
            $path = $request->file('path');
            if ($path) {
                if ($path->isValid()) {
                    $realPath = $path->getRealPath(); //临时文件的绝对路径
                    $ext      = $path->getClientOriginalExtension(); // 扩展名
                    $filename = date('YmdHis') . '_' . uniqid() . '.' . $ext; //文件名
                    $osspath  = 'block_chain/' . $filename; //oss路径
                    $oss->uploadFile(config('alioss.bucket'), $osspath, $realPath); //上传文件到阿里云

                    $path = $osspath; //数据库保存非全路径文件名
                }
                //保存申请表
                ApplyFile::create([
                    'path' => $path,
                ]);
            }
        }
        //保存
        System::updateOrCreate(
            [
                'id' => 1,
            ],
            $data
        );

        return back()->with(['msg' => '修改成功', 'status' => 1]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
